package com.gitee.mp.core.interceptor;

import cn.hutool.core.collection.CollUtil;
import com.gitee.mp.core.connector.MilvusConnector;
import com.gitee.mp.infra.constants.MilvusPlusConstants;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Method;
import java.util.List;

/**
 * @author hongda.li
 */
public class BatchInsertInterceptor extends AbstractMapperInterceptor {

    @Override
    public boolean supported(Method method) {
        return MilvusPlusConstants.MethodName.METHOD_BATCH_INSERT.equals(method.getName());
    }

    @Override
    public void intercept(InterceptorContext context) {
        final MilvusConnector connector = context.getConnector();

        List<?> toInsertList = (List<?>) CollUtil.getFirst(context.getArguments());
        if (CollectionUtils.isEmpty(toInsertList)) {
            context.setResult(0);
            return;
        }

        context.setResult(connector.insert(this.getCollectionName(context),
                toInsertList.stream()
                        .map(this::beanToMap)
                        .map(toInsertMap -> this.transformParams(context, toInsertMap))
                        .toList()));
    }
}
